package com.briup.product_my.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.briup.product_my.domain.ManagerAnimal;
import com.briup.product_my.domain.ManagerBatch;
import com.briup.product_my.domain.ManagerHurdles;
import com.briup.product_my.domain.ManagerQrcode;
import com.briup.product_my.domain.ext.ManagerAnimalExt;
import com.briup.product_my.mapper.ManagerAnimalMapper;
import com.briup.product_my.mapper.ManagerBatchMapper;
import com.briup.product_my.mapper.ManagerHurdlesMapper;
import com.briup.product_my.mapper.ManagerQrcodeMapper;
import com.briup.product_my.mapper.ext.ManagerAnimalExtMapper;
import com.briup.product_my.service.ManagerAnimalService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/8/21-08-21-下午3:07
 * @Description: com.briup.product_my.service.impl
 */
@Service
public class ManagerAnimalServiceImpl implements ManagerAnimalService {
    @Autowired
    private ManagerAnimalMapper managerAnimalMapper;
    @Autowired
    private ManagerAnimalExtMapper managerAnimalExtMapper;
    @Autowired
    private ManagerBatchMapper managerBatchMapper;
    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;
    @Autowired
    private ManagerQrcodeMapper managerQrcodeMapper;
    @Override
    public PageInfo<ManagerAnimalExt> findByPage(Integer pageNum, int pageSize, String aHealthy, String aGender) {
        PageHelper.startPage(pageNum, pageSize);
        List<ManagerAnimalExt> list = managerAnimalExtMapper.findByPage(aHealthy, aGender);
        PageInfo<ManagerAnimalExt> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void saveOrUpdate(ManagerAnimal animal) {
        if(StringUtils.hasText(animal.getAAnimalId())){
                managerAnimalMapper.updateByPrimaryKeySelective(animal);
        }else{
            ManagerBatch managerBatch = managerBatchMapper.selectBy(animal.getABatchId());
            if(managerBatch.getBQuarantine().equals("已检疫")){
                animal.setAStatus("已检疫");
            }else{
                animal.setAStatus("养殖中");
            }
            animal.setAAnimalId(managerAnimalMapper.getMaxId()+1);
            managerAnimalMapper.insert(animal);
        }
    }

    @Override
    public void deleteById(String id) {
        managerAnimalMapper.deleteById(id);
    }

    @Override
    public void deleteByIdAll(String[] ids) {
        managerAnimalMapper.deleteByIdAll(ids);
    }

    @Override
    public void generateQRcode(String animalId) {
        ManagerQrcode managerQrcode1 = managerQrcodeMapper.selectByAnimalId(animalId);
        if(managerQrcode1==null){
            ManagerAnimal animal = managerAnimalMapper.selectById(animalId);
            QrCodeUtil.generate("http://192.168.247.1:9527/#/Code/"+animalId,300,300, FileUtil.file("/C:/Users/x3076/Desktop/briup作业/溯源系统/images/" + animal.getAAnimalId() + ".png"));
            ManagerQrcode managerQrcode = new ManagerQrcode();
            managerQrcode.setQId(managerQrcodeMapper.getMaxId()+1);
            managerQrcode.setQAnimalId(animalId);
            managerQrcode.setQImageUrl("http://localhost:9999/images/"+animalId+".png");
            managerQrcodeMapper.insert(managerQrcode);
        }


    }
}
